package aex.service;



import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import aex.dao.ArticleDao;
import aex.domain.Article;
import aex.util.CU;
import aex.util.PageBean;



@Transactional(readOnly = false)
@Service(ArticleService.SERVICE_NAME)

public class ArticleService  {

	
	public static final String SERVICE_NAME="aex.service.ArticleService";
	/**
	 * 以注解的形式让spring为我们注入dao的实现类
	 */
	@Resource(name = ArticleDao.SERVICE_NAME)
	public ArticleDao dao;
	
	
	
	@Resource(name = ShowPointService.SERVICE_NAME)
	public ShowPointService spService;
	
	public ArticleService() {
		
	}
	public Article findByID(Serializable id) {
		return dao.findObjectByID(Integer.parseInt(id+""));
	}
	public void deleteById(Serializable id,String appName) {
		spService.delPointByTargetId(id);//删除文章之前把文章所占有的展位删除！
		CU.delImageFile(dao.findObjectByID(id).getArAuthor().replaceAll(appName, ""));//去除一个会重复的app名称
		dao.deleteObjectByIDs(Integer.parseInt(id+""));
	}
	public void save(Article obj) {
		dao.save(obj);
	}
	public void update(Article obj) {
		dao.merge(obj);
	}
	public PageBean<Article> findByCategory(PageBean<Article> pageBean,
			String id) {
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arType", "desc");
		orderby.put("tName.arTime", "desc");
		return dao.findPageBean("and tName.articleCategory ="+id , null, orderby, pageBean);
	}
	
	/**
	 * 查询排序后的前一篇文章
	 * @param artId
	 * @param cateId
	 * @return
	 */
	public Article findPrev(int artId, int cateId) {
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arTime", "desc");
		List<Article> list=dao.findCollectionByConditionNoPage(" and tName.articleCategory ="+cateId, null, orderby);
		Article prev=null;
		for (Article article : list) {
			if(article.getArId()==artId&&list.indexOf(article)>0){
				prev=list.get(list.indexOf(article)-1);
			}
		}
		return prev;
	}

	/**
	 * 查询排序后的后一篇文章
	 * @param artId
	 * @param cateId
	 * @return
	 */
	public Article findNext(int artId, Integer cateId) {
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arTime", "desc");
		List<Article> list=dao.findCollectionByConditionNoPage(" and tName.articleCategory ="+cateId, null, orderby);
		Article next=null;
		for (Article article : list) {
			if(article.getArId()==artId&&list.indexOf(article)+1<list.size()){
				next=list.get(list.indexOf(article)+1);
			}
		}
		return next;
	}
	
	public PageBean<Article> serch(PageBean<Article> pageBean, String title,
			String category, String actType) {
		
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arTime", "desc");
		
		//按照小组查询
		String categoryHql="";
		if(category!=null&&!category.equals("0")){
			categoryHql=" and tName.articleCategory="+category;
		}
		
		//按照文章类型查询
		String arTypeHql="";
		if(actType!=null&&!actType.equals("0")){
			arTypeHql=" and tName.arType="+actType;
		}
		
		//按照文章标题查询
		String titleHql="";
		if(title!=null&&!title.equals("")){
			titleHql=" and tName.arTitle like '%"+title+"%'";
		}
		return dao.findPageBean(categoryHql+arTypeHql+titleHql, null, orderby, pageBean);
	}
	
	
	public void deleteById(String id, String appName) {
		spService.delPointByTargetId(Integer.parseInt(id));//删除文章之前把文章所占有的展位删除！
		CU.delImageFile(dao.findObjectByID(Integer.parseInt(id)).getArAuthor().replaceAll(appName, ""));//去除一个会重复的app名称
		dao.deleteObjectByIDs(Integer.parseInt(id));
	}
	
	
	public PageBean<Article> findByUser(PageBean<Article> pageBean,int userId) {
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arTime", "desc");
		return dao.findPageBean(" and tName.puser="+userId, null, orderby, pageBean);
	}
	
	public List<Article> findByKeyWrod(String keyWord) {
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.arTime", "desc");
		return dao.findCollectionByConditionNoPage(" and tName.arTitle like '%"+keyWord+"%'", null, orderby);
	}
	
}
